<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>07.Ajax错误处理</title>
  </head>
  <body>
    <button id="btn">发送 Ajax</button>
  </body>
</html>

<script>
  let btn = document.getElementById("btn");

  btn.onclick = function () {
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:3000/error");
    xhr.send();
    xhr.onload = function () {
      //  返回  获取的 http 状态码
      console.log(xhr.responseText);

      if (xhr.status === 400) {
        alert("error：" + xhr.status);
      }
    };

    xhr.onerror = function () {
      console.log("网络断开了");
    };
  };
</script>
